<?php
namespace app\common\service;

use app\common\util\Upload;
use app\common\util\Util;
use think\File;
use think\Model;

class MediaService extends BaseService
{

    /**
     * 统一上传图片接口
     * @param $sid
     * @param $fid
     * @param File $file
     * @return array
     */
    public function upload($sid, $fid, File $file){
        if(empty($file)){
            return ['status' => 0, 'msg' => '请选择图片'];
        }

        //获取图片基本信息
        $file_info = $file->getInfo();

        //图片大小是否超过限制
        $maxSize = config('image_upload.maxSize');
        if($file_info['size'] > $maxSize){
            return ['status' => 0, 'msg' => '图片大小超过允许范围'];
        }

        //是否允许上传的类型
        $ext_arr = explode('.',$file_info['name']);
        // 取最后一个数组的，防止出现 xxx.xxx.png 这样的情况，引起误判
        $ext = strtolower($ext_arr[count($ext_arr)-1]);
        $exts = explode(',',config('image_upload.exts'));
        if(!in_array($ext, $exts)){
            return ['status' => 0, 'msg' => '不合法的文件'];
        }

        //通过七牛云上传
        $upload_result = Upload::uploadFile();

        if($upload_result == false){
            return ['status' => 0, 'msg' => '上传失败'];
        }

        $savename = $upload_result.'.'.$ext;

        $savepath = Util::qiniu_url($upload_result);

        $sha1 = $file->hash('sha1');

        $md5 = $file->hash('md5');
        $data = [
            'sid' => $sid,
            'name' => $file_info['name'],
            'savename' => $savename,
            'savepath' => $savepath,
            'ext' => $ext,
            'mime' => $file_info['type'],
            'size' => $file_info['size'],
            'md5' => $md5,
            'sha1' => $sha1,
            'create_time' => time(),
            'type' => $file_info['type'],
            'fid' => $fid,
            'status' => 1
        ];

        $media_id = db('media')->insertGetId($data);
//        return $media_id === false ? ['status' => 0, 'msg' => '上传失败'] : ['status' => 1, 'msg' => '上传成功', 'data' => ['id' => $media_id, 'img_url' => $savepath.'?imageslim']];
        return $media_id === false ? ['status' => 0, 'msg' => '上传失败'] : ['status' => 1, 'msg' => '上传成功', 'data' => ['id' => $media_id, 'img_url' => $savepath]];
    }

    /**
     * 获取图片列表
     * @param $sid
     * @param int $fid
     * @param $p
     * @param $pageSize
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getImgList($sid, $fid = -1, $p, $pageSize){
        $where = [];
        $where['sid'] = $sid;
        $where['status'] = 1;
        if($fid > -1){
            $where['fid'] = $fid;
        }

        $count = db('media')->where($where)->count();
        $list = db('media')
                ->field('id,savename,savepath as url')
                ->where($where)
                ->order('id DESC')
                ->limit(($p - 1)*$pageSize, $pageSize)
                ->select();
        foreach($list as &$v2) {

            if (preg_match('/^http:/', $v2['url']) == 0) {
                $v2['url'] = '/Uploads/Images/'.$v2['url'].''.$v2['savename'];
            }
        }





        $data = [
            'content' => $list,
            'page' => ['count' => $count]
        ];
        return $data;
    }

    /**
     * 软删除图片
     * @param $sid
     * @param $media_id
     * @return int
     */
    public function deleteImg($sid, $media_id){
        return db('media')->where(['sid' => $sid, 'id' => $media_id])->setField('status', -1);
    }

    /**
     * 移动图片
     * @param $sid
     * @param $media_id
     * @param $media_folder_id
     * @return int
     */
    public function moveImg($sid, $media_id, $media_folder_id){
        $info = db('media_folder')->where(['sid' => $sid, 'id' => $media_folder_id, 'status' => 1])->find();
        if(empty($info)){
            return ['status' => 0, 'msg' => '不存在此文件目录'];
        }
        $result = db('media')->where(['sid' => $sid, 'id' => $media_id, 'status' => 1])->setField('fid', $media_folder_id);
        return $result ? ['status' => 1, 'msg' => '移动成功'] : ['status' => 0, 'msg' => '移动失败'];
    }
}